﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    internal class DemoIndexer
    {
        static void Main(string[] args)
        {
            var names = new DemoIndexer();
            names[0] = "C语言中文网";
            names[1] = "http://c.biancheng.net/";
            names[2] = "C#教程";
            names[3] = "索引器";
            // 使用带有 int 参数的第一个索引器
            for (int i = 0; i < DemoIndexer.size; i++)
            {
                Console.WriteLine(names[i]);
            }
            // 使用带有 string 参数的第二个索引器
            Console.WriteLine("“C#教程”的索引为：{0}", names["C#教程"]);
            Console.ReadKey();
        }
        static public int size = 10;
        private string[] namelist = new string[size];
       
        public DemoIndexer()
        {
            for (int i = 0; i < size; i++)
                namelist[i] = "NULL";
        }


        public string this[int index]
        {
            get
            {
                string tmp;
                if (index >= 0 && index <= size - 1)
                {
                    tmp = namelist[index];
                }
                else
                {
                    tmp = "";
                }
                return (tmp);
            }
            set
            {
                if (index >= 0 && index <= size - 1)
                {
                    namelist[index] = value;
                }
            }
        }

        //重载
        public int this[string name]
        {
            get
            {
                int index = 0;
                while (index < size)
                {
                    if (namelist[index] == name)
                    {
                        return index;
                    }
                    index++;
                }
                return index;
            }
        }
    }
}
